package com.huawei.hgip.paintgame.adapter;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.huawei.hgip.paintgame.R;
import com.huawei.hgip.paintgame.m.data.Menu;
import com.huawei.hgip.paintgame.util.DBAdapter;

public class AppManageAdapter extends BaseAdapter {

	private ArrayList<Menu> menus = new ArrayList<Menu>();
	private DBAdapter mDBAdapter;
	private OnAppManageNameClickListener mOAMNCListener;

	public AppManageAdapter(ArrayList<Menu> menus, DBAdapter mDBAdapter,
			OnAppManageNameClickListener onAppManageNameClickListener) {
		this.menus = menus;
		this.mDBAdapter = mDBAdapter;
		this.mOAMNCListener = onAppManageNameClickListener;
	}

	@Override
	public int getCount() {
		return menus.size();
	}

	@Override
	public Menu getItem(int position) {
		if (position < menus.size()) {
			return menus.get(position);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		Object obj = getItem(position);
		if (position < menus.size()) {
			return ((Menu) obj).getId();
		} else {
			return 0;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Menu menu = (Menu) getItem(position);
		// 获取界面组件元素
		View rowView = convertView;
		MenuViewCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			rowView = inflater.inflate(R.layout.appmanage_list_item, null);
			viewCache = new MenuViewCache(rowView);
			rowView.setTag(viewCache);
		} else {
			viewCache = (MenuViewCache) rowView.getTag();
		}
		if (menu == null) {
			return rowView;
		}
		TextView states = viewCache.getAppName();
		states.setText(menu.getName());
		states.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mOAMNCListener.onAppManageNameClick(menu);
			}
		});

		final TextView name = viewCache.getAppStates();
		final boolean oldStates = menu.isShow();
		name.setText(oldStates ? "是" : "否");
		name.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mDBAdapter.changeStates(menu.getId(), oldStates);
				name.setText(oldStates ? "否" : "是");
			}
		});

		return rowView;
	}

	class MenuViewCache {

		private View baseView;

		private TextView appName;

		private TextView appStates;

		public MenuViewCache(View baseView) {
			this.baseView = baseView;
		}

		public TextView getAppName() {
			if (appName == null) {
				appName = (TextView) baseView.findViewById(R.id.appmanage_name);
			}
			return appName;
		}

		public TextView getAppStates() {
			if (appStates == null) {
				appStates = (TextView) baseView.findViewById(R.id.appmanage_isShow);
			}
			return appStates;
		}
	}

	public interface OnAppManageNameClickListener {
		public void onAppManageNameClick(Menu menu);
	}

}
